package code.Frame.EPFrame;

import code.Frame.FrameManager;
import code.MYSQL;
import code.Main;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import static code.Main.WINDOW_HEIGHT;
import static code.Main.WINDOW_WIDTH;

public class dishFrame {
    public JPanel panel;
    private JTable info;
    private JButton add;
    private JButton modify;
    private JButton del;
    private JButton back;
    private JLabel title;

    private int sid;

    public dishFrame(int id)
    {
        sid = id;
        back.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                Main.frame.remove(Main.panel_now);
                Main.panel_now = FrameManager.ep_shop;
                FrameManager.shop.refresh_table();
                Main.frame.setContentPane(Main.panel_now);
                Main.panel_now.revalidate();
            }
        });
        add.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                JFrame frame = new JFrame("新增菜品信息");
                Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
                frame.setBounds(((int) screenSize.getWidth() - WINDOW_WIDTH) / 2, ((int) screenSize.getHeight() - WINDOW_HEIGHT) / 2,
                        WINDOW_WIDTH, WINDOW_HEIGHT);
                DIinput input = new DIinput(2, 0, sid);
                frame.setContentPane(input.panel);
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
                Thread thread = new Thread(new Runnable() {
                    @Override
                    public void run()
                    {
                        while (!input.end)
                        {
                            try {
                                Thread.sleep(200);
                            } catch (InterruptedException ex) {
                                throw new RuntimeException(ex);
                            }
                        }
                        frame.dispose();
                        refresh_table();
                    }
                });
                thread.start();
            }
        });
        modify.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                if (info.getSelectedRow() == -1)
                    return ;
                int did = Integer.parseInt((String) info.getValueAt(info.getSelectedRow(), 0));
                JFrame frame = new JFrame("修改菜品信息");
                Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
                frame.setBounds(((int) screenSize.getWidth() - WINDOW_WIDTH) / 2, ((int) screenSize.getHeight() - WINDOW_HEIGHT) / 2,
                        WINDOW_WIDTH, WINDOW_HEIGHT);
                DIinput input = new DIinput(1, did, sid);
                frame.setContentPane(input.panel);
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
                Thread thread = new Thread(new Runnable() {
                    @Override
                    public void run()
                    {
                        while (!input.end)
                        {
                            try {
                                Thread.sleep(200);
                            } catch (InterruptedException ex) {
                                throw new RuntimeException(ex);
                            }
                        }
                        frame.dispose();
                        refresh_table();
                    }
                });
                thread.start();
            }
        });
        del.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                if (info.getSelectedRow() == -1)
                    return ;
                int did = Integer.parseInt((String) info.getValueAt(info.getSelectedRow(), 0));
                try
                {
                    MYSQL.del_dish(did);
                } catch (Exception ex)
                {
                    throw new RuntimeException(ex);
                }
                refresh_table();
            }
        });
    }

    public void refresh_table()
    {
        String[] colNames = {"DID", "菜品", "价格", "销量"};
        String[][] data;
        try
        {
            data = MYSQL.query_shop_dish(sid);
        } catch (SQLException ex)
        {
            throw new RuntimeException(ex);
        }
        DefaultTableModel model = new DefaultTableModel(data, colNames);
        info.setModel(model);
    }
}
